//
//  NRWBuyCarVC.m
//  NRWMall
//
//  Created by 邢毅 on 2018/5/11.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWBuyCarVC.h"
#import "NRWBuyCarFilterTopView.h"
#import "NRWFliterAutoLabelView.h"
#import "NRWCarListCell.h"
#import "NRWCarDetailVC.h"
#import "NRWBuyCarFilterManager.h"
#import "NRWBuyCarSortView.h"
#import "NRWBrandSeriesVC.h"
#import "NRWBuyCarConditionModel.h"
#import "NRWBuyCarPriceView.h"
#import "NRWBuyCarFilterVC.h"
#import "NRWBuyCarListApi.h"
#import "NRWFilterScrollModel.h"
#import "NRWGetCooperationPositionApi.h"
#import "NRWCooperateCityPickView.h"
#import "NRWCooperationCityListModel.h"
#import "NRWSubscribeVC.h"
#import "NRWBuyCarListRectModel.h"

@interface NRWBuyCarVC ()<UITableViewDelegate,UITableViewDataSource, NRWBuyCarSortViewDelegate, NRWBrandSeriesVCDelegate, NRWBuyCarPriceViewDelegate, NRWBuyCarFilterVCDelegate,NRWCooperateCityPickViewDelegate>

@property(nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NRWBuyCarFilterTopView *topView;
@property(nonatomic, strong) NRWFliterAutoLabelView *fliterView;
@property(nonatomic, assign) CGFloat oldY;
@property(nonatomic, assign) BOOL isAnimation;
@property (strong, nonatomic) NSArray<NRWBuyCarSortModel *>* sortContentModelArr; /**< 排序model */
@property (strong, nonatomic) NSArray<NRWBuyCarPriceModel *>* priceModelArr;
@property (strong, nonatomic) NRWBuyCarSortView *sortView;
@property (strong, nonatomic) NRWBuyCarPriceView *priceView;
@property (strong, nonatomic) UIButton *rightBtn;
@property (strong, nonatomic) NRWBuyCarPriceModel *tempPriceModel; /**< 临时记录价格筛选条件的model */
@property (strong, nonatomic) NRWBuyCarListApi *buyCarListApi;
@property (strong, nonatomic) NRWGetCooperationPositionApi *cooperationPositionApi;
@property (strong, nonatomic) NSArray<NRWBuyCarListRectModel *>* dataArray;
@property (nonatomic, assign) BOOL isDrag;
@property (strong, nonatomic) NRWCooperateCityPickView *cityPickerView;
@property (strong, nonatomic) NRWCooperationCityListModel *cityModel;
@property (strong, nonatomic) UIButton *subscribeBtn;
@end

@implementation NRWBuyCarVC

#pragma mark - lifeCycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.buyCarListApi.params.pageIndex = 1;
    self.isAnimation = YES;
    
    [self setupUI];
    
    [self.cooperationPositionApi start];
    
    if (self.isNeedReloadData == NO) {
        [self loadNewData];
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.topView.isCarAuthState = self.conditionModel.isCarAuthState;
    if (self.isNeedReloadData) {
        [self loadNewData];
        if (isNotEmptyString(self.conditionModel.sortModel.sortName)) {
            self.topView.sortingStr = self.conditionModel.sortModel.sortName;
        }
        self.isNeedReloadData = NO;
    }
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
#pragma mark - setupUI
- (void)setupUI {
    
    self.navigationView.title = @"买车";
    self.view.backgroundColor = backColor();
    [self.view addSubview:self.topView];
    [self.view addSubview:self.fliterView];
    
    [self initTableview];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = getColor(@"f6f6f6");
    [self.navigationView addSubview:line];
    self.navigationView.clipsToBounds = YES;
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.navigationView);
        make.bottom.equalTo(self.navigationView).mas_offset(-0.5);
        make.height.mas_equalTo(0.5);
    }];
    
    UIButton *subscribeBtn = [[UIButton alloc] init];
    [subscribeBtn setImage:getImage(@"home_subscribe") forState:UIControlStateNormal];
    [subscribeBtn addTarget:self action:@selector(subscribeBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    subscribeBtn.frame = CGRectMake(SCREEN_WIDTH() - 75 - 12, SCREEN_HEIGHT() - getTabBarHeight() - 75 - 32, 75, 75);
    [self.view addSubview:subscribeBtn];
    [self.view bringSubviewToFront:subscribeBtn];
    
    [self setupRightBtn];
}

-(NRWBuyCarFilterTopView *)topView{
    if (!_topView) {
        _topView = [[[NSBundle mainBundle] loadNibNamed:@"NRWBuyCarFilterTopView" owner:self options:nil] firstObject];
        _topView.frame = CGRectMake(0, getNavHeight(), SCREEN_WIDTH(), 85);
        MJWeakSelf
        _topView.topMenuClickBlock = ^(FilterType type, BOOL isSelected) {
            
            if (type == FilterType_Authenticate) {
                weakSelf.conditionModel.isCarAuthState = isSelected;
                [weakSelf loadNewData];
                
            } else if (type == FilterType_sortingNormal) {
                
                if (weakSelf.sortView.isShow) {
                    [weakSelf.sortView dismiss];
                } else {
                    [weakSelf.sortView setSortContentModelArr:weakSelf.sortContentModelArr selectSortName:weakSelf.conditionModel.sortModel.sortName];
                    [weakSelf.sortView showForView:weakSelf.view];
                }
                [weakSelf.priceView dismiss];
                [weakSelf.cityPickerView disMiss];
            }else if (type == FilterType_brands) {
                
                NRWBrandSeriesVC *vc = [[NRWBrandSeriesVC alloc] init];
                vc.selectBrandModel = weakSelf.conditionModel.brandModel;
                vc.delegate = weakSelf;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                
                [weakSelf.sortView dismiss];
                [weakSelf.priceView dismiss];
                [weakSelf.cityPickerView disMiss];
                
            }else if (type == FilterType_price){
                
                if (weakSelf.priceView.isShow) {
                    [weakSelf.priceView dismiss];
                } else {
                    [weakSelf.priceView showForView:weakSelf.view];
                    [weakSelf.priceView setSelectPriceModel:weakSelf.conditionModel.priceModel priceItemModelArray:weakSelf.priceModelArr];
                }
                
                [weakSelf.sortView dismiss];
                [weakSelf.cityPickerView disMiss];
                
            }else if (type == FilterType_city){
                
                if (weakSelf.cityPickerView.isShow) {
                    [weakSelf.cityPickerView disMiss];
                } else {
                    [weakSelf.cityPickerView show];
					weakSelf.cityPickerView.selectProvinceIndex = weakSelf.conditionModel.cityModel.provinceIndex;
					weakSelf.cityPickerView.selectCityIndex = weakSelf.conditionModel.cityModel.cityIndex;
                }
                [weakSelf.sortView dismiss];
                [weakSelf.priceView dismiss];
                
            }else if (type == FilterType_filter){
                NRWBuyCarFilterVC *vc = [[NRWBuyCarFilterVC alloc] init];
                vc.filterModel = weakSelf.conditionModel.filterModel;
                vc.delegate = weakSelf;
                [weakSelf.navigationController pushViewController:vc animated:YES];
                [weakSelf.sortView dismiss];
                [weakSelf.priceView dismiss];
            }
        };
    }
    return _topView;
}
-(NRWFliterAutoLabelView *)fliterView{
    if (!_fliterView) {
        _fliterView = [[NRWFliterAutoLabelView alloc] initWithFrame:CGRectMake(0, getNavHeight() + 85, SCREEN_WIDTH(),50)];
        _fliterView.isShowClose = NO;
    }
    return _fliterView;
}
- (void)filterViewOpenAnimation{
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.tableview.frame = CGRectMake(0, getNavHeight() + 50 + 85, SCREEN_WIDTH(), SCREEN_HEIGHT() - getNavHeight() - 50 - 85 - getTabBarHeight());
    }];
}

- (void)filterViewCloseAnimation {
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.tableview.frame = CGRectMake(0, getNavHeight() + 85, SCREEN_WIDTH(), SCREEN_HEIGHT() - getNavHeight() - 85 - getTabBarHeight());
    }];
}

-(void)initTableview{
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = backColor();
    [self.tableview registerClass:[NRWCarListCell class] forCellReuseIdentifier:@"NRWCarListCell"];
    self.tableview.tableFooterView = DPView();
    [self.view addSubview:self.tableview];
    self.tableview.frame = CGRectMake(0, getNavHeight() + 85, SCREEN_WIDTH(), SCREEN_HEIGHT() - getNavHeight() - 85 - getTabBarHeight());
    [self.tableview addHeaderRefreshWithTarget:self action:@selector(loadNewData)];
    [self.tableview addFooterRefreshWithTarget:self action:@selector(loadMoreData)];
    self.tableview.estimatedRowHeight = 100;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = false;
    }
}

- (void)setupRightBtn {
    
    self.rightBtn = [[UIButton alloc] init];
    [self.rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:mainColor() forState:UIControlStateNormal];
    self.rightBtn.titleLabel.font = getThemeFont(14);
    [self.rightBtn addTarget:self action:@selector(rightBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationView addSubview:self.rightBtn];
    self.rightBtn.hidden = YES;
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.navigationView.mas_right).offset(-6);
        make.bottom.equalTo(self.navigationView.mas_bottom);
        make.height.width.equalTo(@44);
    }];
}

#pragma mark - network
-(void)loadNewData{
    [self.tableview setContentOffset:CGPointZero animated:NO];
    self.buyCarListApi.params.pageIndex = 1;
    [self loadDataParameter];
}

-(void)loadMoreData{
    self.buyCarListApi.params.pageIndex++;
   [self loadDataParameter];

}
-(void)endRefresh{

    [self.tableview.mj_header endRefreshing];
    [self.tableview.mj_footer endRefreshing];
}

#pragma mark - TableviewDelegate && Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NRWBuyCarListRectModel *rectModel = self.dataArray[indexPath.row];
    NRWCarListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWCarListCell" forIndexPath:indexPath];
    cell.carFrameModel = rectModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Result *rt = self.dataArray[indexPath.row].dataModel;
    NRWCarDetailVC *DetailVc = [[NRWCarDetailVC alloc] init];
    DetailVc.carId = rt.carId;
    DetailVc.thisCarModel = rt;
    [self.navigationController pushViewController:DetailVc animated:YES];
}

#pragma mark - NRWCooperateCityPickViewDelegate
- (void)cooperateCityPickView:(NRWCooperateCityPickView *)view confirmButtonDidClick:(UIButton *)sender withProvinceName:(NSString *)provinceName provinceId:(NSString *)provinceId cityName:(NSString *)cityName cityId:(NSString *)cityId provinceIndex:(NSInteger)provinceIndex cityIndex:(NSInteger)cityIndex{
	
	[view disMiss];
	
    if ([cityId isEqualToString:self.conditionModel.cityModel.cityId]) {
        return;
    }
    
    self.conditionModel.cityModel.provinceId = provinceId;
    self.conditionModel.cityModel.provinceName = provinceName;
    self.conditionModel.cityModel.cityId = cityId;
    self.conditionModel.cityModel.cityName = cityName;
	self.conditionModel.cityModel.provinceIndex = provinceIndex;
	self.conditionModel.cityModel.cityIndex = cityIndex;
    [self loadNewData];
}

#pragma mark BasePickerViewDelegate
- (void)basePickView:(BasePickView *)basePickView confirmButtonDidClick:(UIButton *)sender {
    
    NSInteger provinceIndex = [basePickView.pickerView selectedRowInComponent:0];
    NSInteger cityIndex = [basePickView.pickerView selectedRowInComponent:1];
    
    if (provinceIndex == 0) {
        if (isNotEmptyString(self.conditionModel.cityModel.cityId)) {
            self.conditionModel.cityModel.cityId = nil;
        }
    } else {
        self.conditionModel.cityModel.cityId = self.cityModel.result[provinceIndex - 1].citys[cityIndex].cityId;
    }
    [self loadNewData];
}

//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    // 获取开始拖拽时tableview偏移量
//    _oldY = scrollView.contentOffset.y;
//    self.isDrag = YES;
//}
//-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    self.isDrag = NO;
//}
//
//
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    CGFloat offsetY = scrollView.contentOffset.y;
//    if (self.isDrag == NO) {
//        return;
//    }
//    if (offsetY < _oldY + 10) {
//        //down
//        if (self.isAnimation == YES) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//
//                [UIView animateWithDuration:0.5 animations:^{
//                    self.isAnimation = NO;
//                    [self.navigationView setTitle:@"买车"];
//                    self.navigationView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, getNavHeight());
//                    self->_topView.frame = CGRectMake(0, getNavHeight(), SCREEN_WIDTH(), 85);
//                    self->_fliterView.frame = CGRectMake(0, getNavHeight() + 85, SCREEN_WIDTH(), 50);
//                    self.tableview.frame = CGRectMake(0, getNavHeight() + 50 + 85, SCREEN_WIDTH(), SCREEN_HEIGHT() - getNavHeight() - 50 - 85 + getTabBarHeight());
//                    self.tabBarController.tabBar.alpha = 1;
//                } completion:^(BOOL finished) {
//                    self.isAnimation = YES;
//
//                }];
//            });
//        }
//
//    }else if (offsetY > _oldY + 10){
//        //up
//        if (self.isAnimation == YES) {
//            dispatch_async(dispatch_get_main_queue(), ^{
//                [UIView animateWithDuration:0.5 animations:^{
//                    self.isAnimation = NO;
//                    [self.navigationView setTitle:nil];
//                    self.navigationView.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 20);
//                    self->_topView.frame = CGRectMake(0, 20, SCREEN_WIDTH(), 85);
//                    self->_fliterView.frame = CGRectMake(0, 20 + 85, SCREEN_WIDTH(), 50);
//                    self.tableview.frame = CGRectMake(0, 20 + 85+ 50, SCREEN_WIDTH(), SCREEN_HEIGHT() - 50 - 20 - 85 + getTabBarHeight());
//                    self.tabBarController.tabBar.alpha = 0;
//
//                } completion:^(BOOL finished) {
//                    self.isAnimation = YES;
//
//                }];
//            });
//        }
//
//    }
//}
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    NSIndexPath *index = userInfo[@"index"];
    if ([eventName isEqualToString:@"Close"]) {
        
        NRWFilterScrollModel *model = self.fliterView.modelArray[index.item];
		
        if (model.type == FilterScrollModelTypeBrand) {
            self.conditionModel.brandModel = [[NRWBuyCarBrandModel alloc] init];
            
            self.buyCarListApi.params.brandId = nil;
            self.buyCarListApi.params.seriesId = nil;
        }
        if (model.type == FilterScrollModelTypePrice) {
            self.conditionModel.priceModel = [[NRWBuyCarPriceModel alloc] init];
            
            self.buyCarListApi.params.toRetailPrice = nil;
            self.buyCarListApi.params.fromRetailPrice = nil;
        }
        if (model.type == FilterScrollModelTypeCity) {
            self.conditionModel.cityModel = [[NRWBuyCarCityModel alloc] init];
			self.conditionModel.cityModel.provinceIndex = self.conditionModel.cityModel.cityIndex = -1;
        }
        if (model.type == FilterScrollModelTypeMileage) {
            self.conditionModel.filterModel.mileageFrom = nil;
            self.conditionModel.filterModel.mileageTo = nil;
            self.conditionModel.filterModel.mileageTitle = nil;
            self.conditionModel.filterModel.mileageFirstIndex = 0;
            self.conditionModel.filterModel.mileageLastIndex = [NRWBuyCarFilterManager getBuyCarMileageTitleArray].count - 1;
            
            self.buyCarListApi.params.fromMileage = nil;
            self.buyCarListApi.params.toMileage = nil;
        }
        if (model.type == FilterScrollModelTypeCarAge) {
            self.conditionModel.filterModel.carAgeFrom = nil;
            self.conditionModel.filterModel.carAgeTo = nil;
            self.conditionModel.filterModel.carAgeTitle = nil;
            self.conditionModel.filterModel.carAgeFirstIndex = 0;
            self.conditionModel.filterModel.carAgeLastIndex = [NRWBuyCarFilterManager getBuyCarAgeTitleArray].count - 1;
            
            self.buyCarListApi.params.fromCarAge = nil;
            self.buyCarListApi.params.toCarAge = nil;
        }
        if (model.type == FilterScrollModelTypeCarColor) {
            self.conditionModel.filterModel.carOutColorCode = nil;
            self.conditionModel.filterModel.carOutColorName = nil;
            
            self.buyCarListApi.params.carOutColor = nil;
        }
        if (model.type == FilterScrollModelTypeCarType) {
            self.conditionModel.filterModel.carTypeId = nil;
            self.conditionModel.filterModel.carTypeName = nil;
            
            self.buyCarListApi.params.carTypeId = nil;
        }
        if (model.type == FilterScrollModelTypecarEnviron) {
            self.conditionModel.filterModel.carEnvironName = nil;
            
            self.buyCarListApi.params.carEnviron = nil;
        }
        
        [self loadNewData];
    }
}
#pragma mark - privateMethod
- (void)setApiModelWithConditionModel {
    
    self.buyCarListApi.params.marketId = @"1";
    
    self.buyCarListApi.params.carAuthState = self.conditionModel.isCarAuthState? @"10": @"0";
    
    self.buyCarListApi.params.orderMode = self.conditionModel.sortModel.orderMode;
    self.buyCarListApi.params.orderColunm = self.conditionModel.sortModel.orderColunm;
    
    self.buyCarListApi.params.brandId = self.conditionModel.brandModel.brandId;
    self.buyCarListApi.params.seriesId = self.conditionModel.brandModel.seriesId;
    
    self.buyCarListApi.params.fromRetailPrice = self.conditionModel.priceModel.priceFrom;
    self.buyCarListApi.params.toRetailPrice = self.conditionModel.priceModel.priceTo;
    
    self.buyCarListApi.params.provinceId = self.conditionModel.cityModel.provinceId;
    self.buyCarListApi.params.cityId = self.conditionModel.cityModel.cityId;
    
    self.buyCarListApi.params.fromMileage = self.conditionModel.filterModel.mileageFrom;
    self.buyCarListApi.params.toMileage = self.conditionModel.filterModel.mileageTo;
    
    self.buyCarListApi.params.fromCarAge = self.conditionModel.filterModel.carAgeFrom;
    self.buyCarListApi.params.toCarAge = self.conditionModel.filterModel.carAgeTo;
    
    self.buyCarListApi.params.carOutColor = self.conditionModel.filterModel.carOutColorName;
    
    self.buyCarListApi.params.carTypeId = self.conditionModel.filterModel.carTypeId;
    
    self.buyCarListApi.params.carEnviron = self.conditionModel.filterModel.carEnvironName;
}

- (void)formatScrollModelArray {
    
    NSMutableArray *arrM = [NSMutableArray array];
    
    if (isNotEmptyString(self.conditionModel.brandModel.brandId)) {
        
        NRWFilterScrollModel *model = [[NRWFilterScrollModel alloc] init];
        if (isEmptyString(self.conditionModel.brandModel.seriesId)) {
            model.title = self.conditionModel.brandModel.brandName;
        } else {
            model.title = [NSString stringWithFormat:@"%@ %@", self.conditionModel.brandModel.brandName, self.conditionModel.brandModel.seriesName];
        }
        model.type = FilterScrollModelTypeBrand;
        [arrM addObject:model];
    }
    
    if (isNotEmptyString(self.conditionModel.priceModel.priceTitle)) {
        
        NRWFilterScrollModel *model = [[NRWFilterScrollModel alloc] init];
        model.title = self.conditionModel.priceModel.priceTitle;
        model.type = FilterScrollModelTypePrice;
        [arrM addObject:model];
    }
    
    if (isNotEmptyString(self.conditionModel.cityModel.provinceId) && isNotEmptyString(self.conditionModel.cityModel.cityId)) {
        
        NRWFilterScrollModel *model = [[NRWFilterScrollModel alloc] init];
        model.title = [NSString stringWithFormat:@"%@ %@", self.conditionModel.cityModel.provinceName, self.conditionModel.cityModel.cityName];
        model.type = FilterScrollModelTypeCity;
        [arrM addObject:model];
    }
    
    if (isNotEmptyString(self.conditionModel.filterModel.mileageTitle)) {
        NRWFilterScrollModel *model = [[NRWFilterScrollModel alloc] init];
        model.title = self.conditionModel.filterModel.mileageTitle;
        model.type = FilterScrollModelTypeMileage;
        [arrM addObject:model];
    }
    
    if (isNotEmptyString(self.conditionModel.filterModel.carAgeTitle)) {
        
        NRWFilterScrollModel *model = [[NRWFilterScrollModel alloc] init];
        model.title = self.conditionModel.filterModel.carAgeTitle;
        model.type = FilterScrollModelTypeCarAge;
        [arrM addObject:model];
    }
    
    if (isNotEmptyString(self.conditionModel.filterModel.carOutColorName)) {
        
        NRWFilterScrollModel *model = [[NRWFilterScrollModel alloc] init];
        model.title = self.conditionModel.filterModel.carOutColorName;
        model.type = FilterScrollModelTypeCarColor;
        [arrM addObject:model];
    }
    
    if (isNotEmptyString(self.conditionModel.filterModel.carTypeName)) {
        
        NRWFilterScrollModel *model = [[NRWFilterScrollModel alloc] init];
        model.title = self.conditionModel.filterModel.carTypeName;
        model.type = FilterScrollModelTypeCarType;
        [arrM addObject:model];
    }
    
    if (isNotEmptyString(self.conditionModel.filterModel.carEnvironName)) {
        
        NRWFilterScrollModel *model = [[NRWFilterScrollModel alloc] init];
        model.title = self.conditionModel.filterModel.carEnvironName;
        model.type = FilterScrollModelTypecarEnviron;
        [arrM addObject:model];
    }
    
    self.fliterView.modelArray = arrM.copy;
    
    if (self.fliterView.modelArray.count == 0) {

        [self filterViewCloseAnimation];
    } else {

        [self filterViewOpenAnimation];
    }
}
#pragma mark touchEvent
- (void)subscribeBtnDidClick:(UIButton *)sender {
    
    if ([NRWUserInfoModel shared].isLogin) {
        
        NRWSubscribeVC *vc = [[NRWSubscribeVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        
        MJWeakSelf
        NRWLoginView *loginView = [[NRWLoginView alloc] init];
        loginView.loginSuccessBlock = ^{
            NRWSubscribeVC *vc = [[NRWSubscribeVC alloc] init];
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        [loginView show];
    }
}

#pragma mark - publicMethod
#pragma mark - DelegateMethod
#pragma mark NRWBuyCarSortViewDelegate
- (void)buyCarSortView:(NRWBuyCarSortView *)view sortTitle:(NSString *)sortTitle sortId:(NSString *)sortId {
    
    self.topView.sortingStr = sortTitle;
    
    if (![self.conditionModel.sortModel.sortName isEqualToString:sortTitle]) {// 如果两次选择的不同, 则发送请求. 相同跳过
        self.conditionModel.sortModel.sortName = sortTitle;
        [self loadNewData];
    }
    
    [self.sortView dismiss];
}

#pragma mark NRWBrandSeriesVCDelegate
- (void)brandSeriesVC:(NRWBrandSeriesVC *)view selectBrandId:(NSString *)selectBrandId selectBrandName:(NSString *)selectBrandName selectSeriesId:(NSString *)selectSeriesId selectSeriesName:(NSString *)selectSeriesName {

    if (([self.conditionModel.brandModel.brandId isEqualToString:selectBrandId] && [self.conditionModel.brandModel.seriesId isEqualToString:selectSeriesId]) ||
        (self.conditionModel.brandModel.brandId == nil && selectBrandId == nil)) {
        return;
    }
    
    self.conditionModel.brandModel.brandId = selectBrandId;
    self.conditionModel.brandModel.brandName = selectBrandName;
    self.conditionModel.brandModel.seriesId = selectSeriesId;
    self.conditionModel.brandModel.seriesName = selectSeriesName;
    
    [self loadNewData];
}

#pragma mark - NRWBuyCarPriceViewDelegate
- (void)buyCarPriceView:(NRWBuyCarPriceView *)view didSelectPriceWithPriceModel:(NRWBuyCarPriceModel *)selectPriceModel {
    
    self.tempPriceModel = selectPriceModel;
}

- (void)buyCarPriceViewWillShow:(NRWBuyCarPriceView *)view {
    
    self.rightBtn.hidden = NO;
}

- (void)buyCarPriceViewWillDismiss:(NRWBuyCarPriceView *)view {
    
    self.rightBtn.hidden = YES;
}

#pragma mark - NRWBuyCarFilterVCDelegate
- (void)buyCarFilterVC:(NRWBuyCarFilterVC *)vc filterCompletedWithFilterModel:(NRWBuyCarFilterModel *)filterModel {
    
    if ([self.conditionModel.filterModel isEqualToOtherBaseModel:filterModel]) {
        return;
    }

    self.conditionModel.filterModel = filterModel;

    [self loadNewData];
}


#pragma mark - networkDelegate
-(void)loadDataParameter{
    
    [self setApiModelWithConditionModel];
    [self formatScrollModelArray];
    [self.buyCarListApi start];
}

- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    
    if (request == self.buyCarListApi) {
        
        NRWBuyCarListDataModel *newModel = [NRWBuyCarListDataModel mj_objectWithKeyValues:request.responseObject];
        
        if (newModel.result.count == 0) {
            
            if (self.buyCarListApi.params.pageIndex == 1) {
                
                MJWeakSelf
                [self.tableview showEmptyMaskViewWithClickBlock:^{

                    [weakSelf.buyCarListApi startWithoutCache];
                }];
                self.dataArray = nil;
                [self.tableview.mj_footer resetNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            }
        } else {
            
            [self.tableview removeMaskView];
            if (self.buyCarListApi.params.pageIndex == 1) {
                
                [self.tableview endRefresh];
				
				NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:newModel.result.count];
				for (NSInteger i = 0; i < newModel.result.count; i++) {
					NRWBuyCarListRectModel *model = [[NRWBuyCarListRectModel alloc] init];
					model.dataModel = newModel.result[i];
					[arrM addObject:model];
				}
				self.dataArray = arrM.copy;
                if (self.buyCarListApi.params.pageSize > newModel.result.count) {
                    [self.tableview.mj_footer endRefreshingWithNoMoreData];
                } else {
                    [self.tableview.mj_footer endRefreshing];
                }
            } else {
                NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.dataArray];
				for (NSInteger i = 0; i < newModel.result.count; i++) {
					NRWBuyCarListRectModel *model = [[NRWBuyCarListRectModel alloc] init];
					model.dataModel = newModel.result[i];
					[arrM addObject:model];
				}
                self.dataArray = arrM.copy;
                
                if (newModel.result.count == self.buyCarListApi.params.pageSize) {
                    
                    [self.tableview.mj_footer resetNoMoreData];
                    [self.tableview.mj_footer endRefreshing];
                } else {
                    
                    [self.tableview.mj_footer endRefreshingWithNoMoreData];
                }
            }
        }
        [self.tableview reloadData];
    } else if (request == self.cooperationPositionApi) {
        
        self.cityModel = [NRWCooperationCityListModel mj_objectWithKeyValues:self.cooperationPositionApi.responseObject];
        
        self.cityPickerView.cityModel = self.cityModel;
    }
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    [self endRefresh];
    [NRWMessageHUD showMessage:@"请求失败" forTime:2.0 withRemindId:remindFail];

}
#pragma mark - touchEventAction
- (void)rightBtnDidClick:(UIButton *)sender {
    
    [self.priceView dismiss];
    
    if ([self.conditionModel.priceModel.priceFrom isEqualToString:self.tempPriceModel.priceFrom] && [self.conditionModel.priceModel.priceTo isEqualToString:self.tempPriceModel.priceTo]) {
        return;
    }

    self.conditionModel.priceModel = self.tempPriceModel;
    
    [self loadNewData];
}
#pragma mark - setter

#pragma mark - getter
- (NSArray<NRWBuyCarSortModel *> *)sortContentModelArr {
    
    if (_sortContentModelArr == nil) {
        _sortContentModelArr = [NRWBuyCarFilterManager getSortContentModelArray];
    }
    return _sortContentModelArr;
}

- (NSArray<NRWBuyCarPriceModel *> *)priceModelArr {
    
    if (_priceModelArr == nil) {
        _priceModelArr = [NRWBuyCarFilterManager getPriceModelArray];
    }
    return _priceModelArr;
}

- (NRWBuyCarSortView *)sortView {
    
    if (_sortView == nil) {
        _sortView = [[NRWBuyCarSortView alloc] init];
        _sortView.delegate = self;
        _sortView.frame = CGRectMake(0, self.topView.viewMaxY, SCREEN_WIDTH(), SCREEN_HEIGHT() - self.topView.viewMaxY);
    }
    return _sortView;
}

- (NRWBuyCarPriceView *)priceView {
    
    if (_priceView == nil) {
        _priceView = [[NRWBuyCarPriceView alloc] init];
        _priceView.delegate = self;
        _priceView.frame = CGRectMake(0, self.topView.viewMaxY, SCREEN_WIDTH(), SCREEN_HEIGHT() - self.topView.viewMaxY);
    }
    return _priceView;
}

- (NRWBuyCarConditionModel *)conditionModel {
    
    if (_conditionModel == nil) {
        _conditionModel = [[NRWBuyCarConditionModel alloc] init];
    }
    return _conditionModel;
}

- (NRWGetCooperationPositionApi *)cooperationPositionApi {
    
    if (_cooperationPositionApi == nil) {
        _cooperationPositionApi = [[NRWGetCooperationPositionApi alloc] init];
        _cooperationPositionApi.delegate = self;
    }
    return _cooperationPositionApi;
}

- (NRWCooperateCityPickView *)cityPickerView {
    
    if (_cityPickerView == nil) {
        _cityPickerView = [[NRWCooperateCityPickView alloc] init];
        _cityPickerView.delegate = self;
    }
    return _cityPickerView;
}

-(NRWBuyCarListApi *)buyCarListApi{
    if (!_buyCarListApi) {
        _buyCarListApi = [[NRWBuyCarListApi alloc] init];
        _buyCarListApi.delegate = self;
    }
    return _buyCarListApi;
}

//-(NSArray<Result *> *)dataArray{
//    if (!_dataArray) {
//        _dataArray = [NSArray array];
//    }
//    return _dataArray;
//}

@end
